package com.lxb.blog.web;


import com.lxb.blog.entity.Blog;
import com.lxb.blog.service.BlogService;
import com.lxb.blog.service.TagService;
import com.lxb.blog.service.TypeService;
import com.lxb.blog.util.MarkdownUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.misc.resources.Messages_pt_BR;

/**
 * 博客首页
 */
@Controller
public class IndexController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;

    @Autowired
    private TypeService typeService;

    public static  String queryStatic = "";

    /**
     * 首页
     * @return
     */
    @GetMapping("/")
    public  String index(@PageableDefault(size = 4,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                     Pageable pageable,
                         Model model){
        model.addAttribute("tags",tagService.listTagTop(4));
        model.addAttribute("types",typeService.listTypeTop(4));
        model.addAttribute("page",blogService.listBlog(pageable));
        model.addAttribute("recommendBlogs",blogService.listRecommendBlogTop(4));
        return  "index";
    }

    @GetMapping("/blog")
    public  String blog(){
        return  "blog";
    }

    /**
     * 搜索
     * @param query
     * @param pageable
     * @param model
     * @return
     */
    @PostMapping("/search")
    public String searchPost(@RequestParam String query,
                         @PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                 Pageable pageable,
                         Model model){
        queryStatic = query;
        model.addAttribute("page",blogService.listBlog("%"+query+"%",pageable));
        model.addAttribute("query",query);
        return "search";
    }

    @GetMapping("/search")
    public String searchGet(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                 Pageable pageable,
                         Model model){
        model.addAttribute("page",blogService.listBlog("%"+queryStatic+"%",pageable));
        model.addAttribute("query",queryStatic);
        return "search";
    }

    /**
     * 博客详情
     * @return
     */
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Integer id,Model model){
        Blog blog = blogService.getBlog(id);
        blogService.updateView(id);
        String content = blog.getContent();
        //调用MarkdownUtils.markdownToHtmlExtensions()方法把Markdown转化为html
        Blog b = new Blog();
        BeanUtils.copyProperties(blog,b);
        b.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        model.addAttribute("blog",b);

        return "blog";
    }

}
